package com.pzhu.view;


import com.pzhu.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


public class ShowServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //设置响应编码格式
        resp.setContentType("text/html;charset=utf-8");
        //获取Session及其中的User数据信息
        HttpSession hs=req.getSession();
        User u=(User) hs.getAttribute("userId");

        //假如过了1天才访问show页面，Session已经销毁了，请求转发到CookieServlet，重建Session装入user数据
        if(u==null){
            //重定向
            resp.sendRedirect("/ss/lbc");
            return;
        }
        //输出获取到的Session存储的u对象用户信息
        resp.getWriter().write("<html>");
        resp.getWriter().write("<head>");
        resp.getWriter().write("</head>");
        resp.getWriter().write("<body>");
        resp.getWriter().write("<table border='1px'>");
        resp.getWriter().write("<tr>");
        resp.getWriter().write("<td>Uid</td>");
        System.out.println(u.getUid());
        resp.getWriter().write("<td>"+u.getUid()+"</td>");
        resp.getWriter().write("</tr>");
        resp.getWriter().write("<tr>");
        resp.getWriter().write("<td>Uname</td>");
        System.out.println(u.getUname());
        resp.getWriter().write("<td>"+u.getUname()+"</td>");
        resp.getWriter().write("</tr>");
        resp.getWriter().write("<tr>");
        resp.getWriter().write("<td>Pwd</td>");
        System.out.println(u.getPwd());
        resp.getWriter().write("<td>"+u.getPwd()+"</td>");
        resp.getWriter().write("</tr>");
        resp.getWriter().write("</table>");
        resp.getWriter().write("</body>");
        resp.getWriter().write("</html>");
    }

}
